package com.remist.service.impl;

import java.util.LinkedList;
import java.util.List;

import com.remist.dao.ProductTypeDao;
import com.remist.model.ProductType;
import com.remist.service.ProductTypeService;

public class ProductTypeServiceImpl implements ProductTypeService {

	private ProductTypeDao productTypeDao;
	
	public ProductTypeDao getProductTypeDao() {
		return productTypeDao;
	}

	public void setProductTypeDao(ProductTypeDao productTypeDao) {
		this.productTypeDao = productTypeDao;
	}

	public ProductType addProductType(ProductType productTypeBean) {
		if ( productTypeBean.getParentId() == 0 || 
			 productTypeBean.getName() ==null ||
			 productTypeBean.getName() .equals("") )
			return null;
		else {
			return productTypeDao.addProductType(productTypeBean);
		}
	}

	public boolean updateProductType(ProductType productTypeBean) {
		return productTypeDao.updateProductType(productTypeBean);
	}

	public boolean deleteProductType(ProductType productTypeBean) {
		return productTypeDao.deleteProductType(productTypeBean);
	}

	public List<ProductType> listSonProductTypeByParentKey(ProductType productTypeBean) {
		return productTypeDao.listSonProductTypeByParentKey(productTypeBean);
	}

	public ProductType getProductTypeByKey(ProductType bean) {
		return productTypeDao.getProductTypeByKey(bean);
	}

	public ProductType getParentTypeByParentKey(ProductType bean) {
		return productTypeDao.getParentTypeByParentKey(bean);
	}

	public LinkedList<ProductType> listAllParents(ProductType bean) {
		LinkedList<ProductType> linkLs = new LinkedList<ProductType>();
		ProductType parent = bean;
		while(true){
			parent = getParentTypeByParentKey( parent );
			if ( parent != null)
				linkLs.push(parent);
			else
				break;
		}
		return linkLs;
	}
	public List<ProductType> listChilds(ProductType bean) {
		return productTypeDao.listChilds(bean);
//		LinkedList<ProductType> linkLs = new LinkedList<ProductType>();
//		ProductType parent = bean;
//		while(true){
//			parent = getParentTypeByParentKey( parent );
//			if ( parent != null)
//				linkLs.push(parent);
//			else
//				break;
//		}
//		return linkLs;
	}
	
	public ProductType getProductTypeByName(ProductType bean) {
		return productTypeDao.getProductTypeByName(bean);
	}
}
